/*****************************************************************************
 *   Copyright 2011 ZHUANG Yuan              	                             *
 *   SudokuITS as Final Year Project for BScCS, City University of Hong Kong *
 *                                      									 *
 *   Licensed under the Apache License, Version 2.0 (the "License");         *
 *   you may not use this file except in compliance with the License.        *
 *   You may obtain a copy of the License at                                 *
 *                                                                           *
 *   http://www.apache.org/licenses/LICENSE-2.0                              *
 *                                                                           *
 *   Unless required by applicable law or agreed to in writing, software     *
 *   distributed under the License is distributed on an "AS IS" BASIS,       *
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
 *   See the License for the specific language governing permissions and     *
 *   limitations under the License.                                          *
 *****************************************************************************/

package android.sudoku.its.views;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.sudoku.its.sqlite.SQLiteHandler;
import android.sudoku.its.sqlite.StudentTable;
import android.sudoku.its.student.StudentUtil;
import android.sudoku.its.student.Student;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

/**
 * Register Page
 * @author 51158282
 *
 */
public class RegisterPage extends Activity implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		
		// Set layout for Register Page
		setContentView(R.layout.register);
		
		// Set education level spinner
	    Spinner educationLevelSpinner = (Spinner) findViewById(R.id.education_level_spinner);
	    ArrayAdapter<CharSequence> educationLevelAdapter = ArrayAdapter.createFromResource(
	    		this, R.array.education_levels, R.layout.spinner_layout);
	    educationLevelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    educationLevelSpinner.setAdapter(educationLevelAdapter);
	    
	    // Set subject spinner
	    Spinner subjectSpinner = (Spinner) findViewById(R.id.subject_spinner);
	    ArrayAdapter<CharSequence> subjectlAdapter = ArrayAdapter.createFromResource(
	    		this, R.array.subjects, R.layout.spinner_layout);
	    subjectlAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    subjectSpinner.setAdapter(subjectlAdapter);
		
		// Set onClickListener for buttons
		ImageView registerButton = (ImageView) findViewById(R.id.register_page_register_button);
		registerButton.setOnClickListener(this);
		ImageView cancelButton = (ImageView) findViewById(R.id.register_page_cancel_button);
		cancelButton.setOnClickListener(this);
	}
	
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.register_page_register_button:
				registerNewStudent();
				startMainMenuPage();
				break;
			case R.id.register_page_cancel_button:
				this.finish();
				break;
		}		
	}
	
	/**
	 * Add information about a new student to the database
	 */
	private void registerNewStudent() {		
		// Get user input name
		EditText nameTV = (EditText) findViewById(R.id.name_text);
		String name = nameTV.getText().toString();
		
		// Get user input password
		EditText pwdTV = (EditText) findViewById(R.id.password_text);
		String pwd = pwdTV.getText().toString();
		
		// Get user selected gender
		RadioGroup genderRG = (RadioGroup) findViewById(R.id.gender_radio_group);
		RadioButton genderRB = (RadioButton) findViewById(genderRG.getCheckedRadioButtonId());
		String gender = genderRB.getText().toString();
		
		// Get user input age
		EditText ageTV = (EditText) findViewById(R.id.age_text);
		int age = Integer.parseInt(ageTV.getText().toString());
		
		// Get user input Sudoku experience
		EditText sudokuExperienceTV = (EditText) findViewById(R.id.sudoku_experience_text);
		int sudokuExperience = Integer.parseInt(sudokuExperienceTV.getText().toString());
		
		StudentUtil studentUtil = new StudentUtil();
		int level = studentUtil.determineStudentLevel(sudokuExperience);
		
		// Get user education level
		Spinner educationLevelSpinner = (Spinner)findViewById(R.id.education_level_spinner);
		String educationLevel = educationLevelSpinner.getSelectedItem().toString();
		
		// Get science or art
		Spinner subjectSpinner = (Spinner)findViewById(R.id.subject_spinner);
		String subject = subjectSpinner.getSelectedItem().toString();
		
		SQLiteHandler sqliteHandler = new SQLiteHandler(this);
		StudentTable studentTable = new StudentTable();
		Student student = new Student(name, pwd, gender, age, sudokuExperience, level, educationLevel, subject, 0, 0, 0, 0, 0, 0); 
		studentTable.addStudent(sqliteHandler, student);
	}
	
	/**
	 * Go to Main Menu Page
	 */
	private void startMainMenuPage() {
		// Get user input name
		EditText nameTV = (EditText) findViewById(R.id.name_text);
		String name = nameTV.getText().toString();
		
		Intent i =  new Intent(getBaseContext(), MainMenuPage.class);
		i.putExtra(MainMenuPage.USER_TITLE, name);
		i.putExtra(MainMenuPage.USER_TYPE, MainMenuPage.NEW_USER);
		startActivity(i);		
	}
}
